/**
 * 
 */
package com.kahweh.ngphoto.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.kahweh.ngphoto.client.bean.Property;

/**
 * @author Michael
 *
 */
public class PropertyManager implements EntryPoint {

    private VerticalPanel mainPanel = new VerticalPanel();

    private FlexTable pTable = new FlexTable();
    
    private FlexTable editTable = new FlexTable();
    private SuggestBox section = new SuggestBox();
    private TextBox name = new TextBox();
    private ListBox type = new ListBox();
    private TextBox value = new TextBox();
    private Button submitButton = new Button();
    
    private PropertyServiceAsync propertyService = (PropertyServiceAsync)GWT.create(PropertyService.class);

    /* (non-Javadoc)
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    @Override
    public void onModuleLoad() {
        // TODO Auto-generated method stub
    	initPTable();

        editTable.setText(0, 0, "Section");
        editTable.setWidget(0, 1, section);
        editTable.setText(1, 0, "Value Name");
        editTable.setWidget(1, 1, name);
        editTable.setText(2, 0, "Value Type");
        editTable.setWidget(2, 1, type);
        editTable.setText(3, 0, "Value");
        editTable.setWidget(3, 1, value);
        editTable.setWidget(4, 1, submitButton);

        type.addItem("String");
        type.addItem("Long");
        type.addItem("Date");
        type.setItemSelected(0, true);
        submitButton.setText("Edit/Create");

        mainPanel.add(pTable);
        mainPanel.add(editTable);
        RootPanel.get("mainDiv").add(mainPanel);

        
        section.setFocus(true);

        submitButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            	submitCreateAndUpdate();
            }
        });
        
        propertyService.propertyService(2, null, propertyCallback);
    }
    
    private void submitCreateAndUpdate() {
        if (!validateValues()) return;
        Property p = new Property();
        p.setSection(section.getValue());
        p.setName(name.getValue());
        p.setType(type.getValue(type.getSelectedIndex()));
        p.setValue(value.getValue());
        propertyService.propertyService(0, p, propertyCallback);
    }

    private boolean validateValues() {
    	if (name.getValue().trim().equals("")) {
    		name.setFocus(true);
    		Window.alert("value name cannot be empty!");
    	    return false;
    	}
    	return true;
    }
    
    private AsyncCallback<List<Property>> propertyCallback = new AsyncCallback<List<Property>>() {

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub
			Window.alert(caught.toString());
		}

		@Override
		public void onSuccess(List<Property> result) {
			if (result == null) {
				Window.alert("Please Login with admin_account first!");
				return;
			}
			
			initPTable();
			for (int i = 0; i<result.size(); i++) {
				Property p = result.get(i);
				pTable.setText(i+1, 0, p.getSection());
				pTable.setText(i+1, 1, p.getName());
				pTable.setText(i+1, 2, p.getType());
				pTable.setText(i+1, 3, p.getValue());
				HorizontalPanel panel = new HorizontalPanel();
				Button selectBtn = new Button();
				selectBtn.setText("Select");
				final int fi = i+1;
				selectBtn.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent evt) {
						section.setValue(pTable.getText(fi, 0));
						name.setValue(pTable.getText(fi, 1));
						type.setSelectedIndex(getTypeIndex(pTable.getText(fi, 2)));
						value.setValue(pTable.getText(fi, 3));
						value.setFocus(true);
					}
				});

				Button deleteBtn = new Button();
				deleteBtn.setText("Delete");
				final long id = p.getId();
				deleteBtn.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent evt) {
						propertyService.propertyService(1, new Property(id, "", "", "", ""), propertyCallback);
					}
				});

				panel.add(selectBtn);
				panel.add(deleteBtn);
				pTable.setWidget(i+1, 4, panel);
			}
			cleanCreateForm();
		}
    };
    
    private int getTypeIndex(String s) {
    	if ("String".equalsIgnoreCase(s)) return 0;
    	if ("Long".equalsIgnoreCase(s)) return 1;
    	if ("Date".equalsIgnoreCase(s)) return 2;
    	return -1;
    }
    
    private void initPTable() {
    	pTable.removeAllRows();
        pTable.setText(0, 0, "Section");
        pTable.setText(0, 1, "Name");
        pTable.setText(0, 2, "Value");
        pTable.setText(0, 3, "Type");
        pTable.setText(0, 4, "Manipulate");
        pTable.setBorderWidth(1);
    }
    
    private void cleanCreateForm() {
    	section.setValue("");
    	name.setValue("");
    	type.setSelectedIndex(0);
    	value.setValue("");
    }
}
